/*
 * Copyright (C) 2011, EADS France
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.gmi.controller;

/**
 *
 * @author Laurent WOUTERS
 */
public class Command extends xowl.gmi.environment.UIAction {
    private xowl.utils.logging.Logger log;
    private xowl.interpreter.Repository repository;
    private xowl.interpreter.Transformer transformer;
    private xowl.interpreter.Changeset changeset;
    private xowl.interpreter.Transaction transaction;
    
    public Command(String name, xowl.utils.logging.Logger log, xowl.interpreter.Repository repository, xowl.interpreter.Transformer transformer, xowl.interpreter.Changeset changeset) {
        super(name);
        this.log = log;
        this.repository = repository;
        this.transformer = transformer;
        this.changeset = changeset;
    }
    
    public void execute() {
        try {
            changeset = transformer.tranform(changeset);
            repository.beginTransaction(description);
            repository.apply(changeset);
            transaction = repository.endTransaction();
        }
        catch (xowl.interpreter.XOWLException ex) { log.error(Controller.name, "Error while executing UI command!", ex); }
    }
    public void undo() {
        try { transaction.undo(); }
        catch (xowl.interpreter.XOWLException ex) { log.error(Controller.name, "Error while undoing UI command!", ex); }
    }
    public void redo() {
        try { transaction.redo(); }
        catch (xowl.interpreter.XOWLException ex) { log.error(Controller.name, "Error while redoing UI command!", ex); }
    }
}
